/*:
 * @plugindesc patch to SAN_AnalogMove ver1.4.3 : Movement of pixel.【像素移动补丁】
 * @author doranikofu(edited by 老饕)
 * @date 2020.3.4
 
 * @help
 * 本插件是SAN_AnalogMove ver1.4.3的补丁。
 * 修正“像素移动”及“八方向行走”时，与事件触发相关的问题。
*/


// =====================================================
// 角色斜向面向/背对主角（支持像素移动、八方向）
// =====================================================
// 面向
Game_Character.prototype.turnTowardCharacter = function(character) {
    var sx = this.deltaXFrom(character._realX);
    var sy = this.deltaYFrom(character._realY);
	var radian = Math.atan2(sx,sy) + Math.PI;
	this.setDirection(
        radian < Math.PI / 8.0 *  1.0 ? 2 :
        radian < Math.PI / 8.0 *  3.0 ? 3 :
        radian < Math.PI / 8.0 *  5.0 ? 6 :
        radian < Math.PI / 8.0 *  7.0 ? 9 :
        radian < Math.PI / 8.0 *  9.0 ? 8 :
        radian < Math.PI / 8.0 * 11.0 ? 7 :
        radian < Math.PI / 8.0 * 13.0 ? 4 :
        radian < Math.PI / 8.0 * 15.0 ? 1 : 2
		);
};
// 背对
Game_Character.prototype.turnAwayFromCharacter = function(character) {
    var sx = this.deltaXFrom(character._realX);
    var sy = this.deltaYFrom(character._realY);
	var radian = Math.atan2(sx,sy) + Math.PI;
	this.setDirection(
        radian < Math.PI / 8.0 *  1.0 ? 8 :
        radian < Math.PI / 8.0 *  3.0 ? 7 :
        radian < Math.PI / 8.0 *  5.0 ? 4 :
        radian < Math.PI / 8.0 *  7.0 ? 1 :
        radian < Math.PI / 8.0 *  9.0 ? 2 :
        radian < Math.PI / 8.0 * 11.0 ? 3 :
        radian < Math.PI / 8.0 * 13.0 ? 6 :
        radian < Math.PI / 8.0 * 15.0 ? 9 : 8
		);
};

// =====================================================
// 扩展事件触发区域
// =====================================================

// 根据角色朝向计算面对的X,Y坐标（支持八方向）
Game_Map.prototype.roundXWithDirection = function(x, d) {
    return this.roundX(x + (d%3 == 0 ? 1 : (d+2)%3 == 0 ? -1 : 0));
};

Game_Map.prototype.roundYWithDirection = function(y, d) {
    return this.roundY(y + (d < 4 ? 1 : d > 6 ? -1 : 0));
};


// 根据角色朝向计算角色左前方的坐标
 Game_Map.prototype.roundXWithDirectionL = function(x, d) {
		switch(d) {
			case 1:    
				return this.roundX(x+1);
				break;
			case 2:   
				return this.roundX(x+1);
				break;
			case 8:   
				return this.roundX(x-1);
				break;
			case 9:   
				return this.roundX(x-1);
				break;
			default :
				return this.roundX(x);
		};
};

Game_Map.prototype.roundYWithDirectionL = function(y, d) {
		switch(d) {
			case 3:    
				return this.roundY(y-1);
				break;
			case 4:   
				return this.roundY(y+1);
				break;
			case 6:   
				return this.roundY(y-1);
				break;
			case 7:   
				return this.roundY(y+1);
				break;
			default :
				return this.roundY(y);
		};
};

// 根据角色朝向计算角色右前方的坐标
 Game_Map.prototype.roundXWithDirectionR = function(x, d) {
		switch(d) {
			case 2:    
				return this.roundX(x-1);
				break;
			case 3:   
				return this.roundX(x-1);
				break;
			case 7:   
				return this.roundX(x+1);
				break;
			case 8:   
				return this.roundX(x+1);
				break;
			default :
				return this.roundX(x);
		};
};

Game_Map.prototype.roundYWithDirectionR = function(y, d) {
		switch(d) {
			case 1:    
				return this.roundY(y-1);
				break;
			case 4:   
				return this.roundY(y-1);
				break;
			case 6:   
				return this.roundY(y+1);
				break;
			case 9:   
				return this.roundY(y+1);
				break;
			default :
				return this.roundY(y);
		};
};


// =====================================================
// 事件触发相关
// =====================================================

// 检查玩家当前位置（更新支持像素移动位置），判断是否触发事件
Game_Player.prototype.checkEventTriggerHere = function(triggers) {
	
    if (this.canStartLocalEvents()) {
        this.startMapEvent(Math.round(this._realX), Math.round(this._realY), triggers, false);
    }
};

// 检查玩家朝向位置（更新支持像素移动位置、八方向），判断是否触发事件（扩展柜台判定）
Game_Player.prototype.checkEventTriggerThere = function(triggers) {
    if (this.canStartLocalEvents()) {
        var direction = this.direction();
        var x1 = Math.round(this._realX);
        var y1 = Math.round(this._realY);
        var x2 = Math.floor($gameMap.roundXWithDirection(x1, direction));
        var y2 = Math.floor($gameMap.roundYWithDirection(y1, direction));
        this.startMapEvent(x2, y2, triggers, true);
		//【正对】
		//若主角（1）朝向（2）是柜台
        if (!$gameMap.isAnyEventStarting() && $gameMap.isCounter(x2, y2)) {
			// 执行柜台后面，主角面向的事件（3）
            var x3 = Math.floor($gameMap.roundXWithDirection(x2, direction));
            var y3 = Math.floor($gameMap.roundYWithDirection(y2, direction));
            this.startMapEvent(x3, y3, triggers, true);	
        }
		//【左前方】
		// 若主角（1）朝向方向（2）的左侧（2l）为柜台		
        var x2l = Math.floor($gameMap.roundXWithDirectionL(x2, direction));
        var y2l = Math.floor($gameMap.roundYWithDirectionL(y2, direction));
		if (!$gameMap.isAnyEventStarting() && $gameMap.isCounter(x2l, y2l)) {
			// 执行主角（1）左前方（2l）后面（3l）的事件
			var x3l = Math.floor($gameMap.roundXWithDirection(x2l, direction));
			var y3l = Math.floor($gameMap.roundYWithDirection(y2l, direction));
			this.startMapEvent(x3l, y3l, triggers, true);
		}
		//【右前方】
		// 若主角（1）朝向方向（2）的右侧（2r）为柜台		
		var x2r = Math.floor($gameMap.roundXWithDirectionR(x2, direction));
		var y2r = Math.floor($gameMap.roundYWithDirectionR(y2, direction));
		if (!$gameMap.isAnyEventStarting() && $gameMap.isCounter(x2r, y2r)) {
			// 执行主角（1）右前方（2l）后面（3r）的事件
			var x3r = Math.floor($gameMap.roundXWithDirection(x2r, direction));
			var y3r = Math.floor($gameMap.roundYWithDirection(y2r, direction));
			this.startMapEvent(x3r, y3r, triggers, true);
		}

    }
};

// 触摸触发器（修正柜台处理）
Game_Player.prototype.triggerTouchAction = function() {
    if ($gameTemp.isDestinationValid()){
        var direction = this.direction();
        var x1 = this.x;
        var y1 = this.y;
        var x2 = $gameMap.roundXWithDirection(x1, direction);
        var y2 = $gameMap.roundYWithDirection(y1, direction);
        var x3 = $gameMap.roundXWithDirection(x2, direction);
        var y3 = $gameMap.roundYWithDirection(y2, direction);

        var x2l = $gameMap.roundXWithDirectionL(x2, direction);
        var y2l = $gameMap.roundYWithDirectionL(y2, direction);
        var x3l = $gameMap.roundXWithDirection(x2l, direction);
        var y3l = $gameMap.roundYWithDirection(y2l, direction);

        var x2r = $gameMap.roundXWithDirectionR(x2, direction);
        var y2r = $gameMap.roundYWithDirectionR(y2, direction);
        var x3r = $gameMap.roundXWithDirection(x2r, direction);
        var y3r = $gameMap.roundYWithDirection(y2r, direction);

        var destX = $gameTemp.destinationX();
        var destY = $gameTemp.destinationY();

		// 排除长按情况
		if (!TouchInput.isLongPressed()){
			// 玩家到达目的地，则触发所在地事件
			if (destX === x1 && destY === y1) {
				return this.triggerTouchActionD1(x1, y1);
			// 若目的地是玩家面向的地方，则触发面向点的事件（交通工具）
			} else if (destX === x2 && destY === y2) {
				return this.triggerTouchActionD2(x2, y2);
			// 若目的地是玩家面向的2格位置，则触发2格后的事件（柜台）
			} else{
				if (destX === x3 && destY === y3)
					return this.triggerTouchActionD3(x2, y2);
				if (destX === x3l && destY === y3l)
					return this.triggerTouchActionD3(x2l, y2l);
				if (destX === x3r && destY === y3r)
					return this.triggerTouchActionD3(x2r, y2r);
			}
	}
    }
    return false;
};

// =====================================================
// set 1/2 scroll for reduced map size --doranikofu
// =====================================================
Game_Map.prototype.parallaxOx = function() {
    if (this._parallaxZero) {
        return Math.floor(this._parallaxX * this.tileWidth());
    } else if (this._parallaxLoopX) {
        return this._parallaxX * this.tileWidth() / 2;
    } else {
        return 0;
    }
};

Game_Map.prototype.parallaxOy = function() {
    if (this._parallaxZero) {
        return Math.floor(this._parallaxY * this.tileHeight());
    } else if (this._parallaxLoopY) {
        return this._parallaxY * this.tileHeight() / 2;
    } else {
        return 0;
    }
};
